<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ax="http://anthill.com.ar/2009/ax">
	<xsl:output method="html" version="1.0" encoding="UTF-8"  indent="yes"/> <!--doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"-->

	<!-- SUPPORT ANY NODE -->

	<xsl:template match="@*|node()">
	  <xsl:copy>
	    <xsl:apply-templates select="@*|node()"/>
	  </xsl:copy>
	</xsl:template>

	<!-- SHORTCUT TEMPLATES -->

	<xsl:template match="ax:project">
	<html>
	    <head>
		<title><xsl:value-of select="@title"/></title>

		<script type="text/javascript" src="http://www.google.com/jsapi"></script>
		<script type="text/javascript" src="js/anthill.js"></script>
		<script type="text/javascript" src="js/anthill.visualization.Tree.js"></script>
		<script type="text/javascript" src="js/anthill.layout.RPane.js"></script>
		<script type="text/javascript" src="js/anthill.layout.Tabs.js"></script>
		<script type="text/javascript" src="js/anthill.layout.SPane.js"></script>
		
		<link rel="stylesheet" media="screen" type="text/css" href="css/ax.css"/>
		
		<xsl:apply-templates select="child::*[name() != 'body']"/>
	    </head>
	    <xsl:apply-templates select="child::*[name() = 'body']"/>
	</html>
	</xsl:template>

	<!-- MAIN TEMPLATES (packages, visualizations, gadgets, layouts, etc.) -->

	<xsl:template match="ax:group">
		<script>
	/*
	 * Processing Group
	 */	
		google.load("visualization", "1", {<xsl:if test="@packages | ax:package">'packages':[<xsl:choose><xsl:when test="@packages"><xsl:value-of select="@packages"/></xsl:when><xsl:otherwise><xsl:for-each select="ax:package">'<xsl:value-of select="@name"/>'<xsl:if test="position() != last()">, </xsl:if></xsl:for-each></xsl:otherwise></xsl:choose>], </xsl:if>'callback': groupCallback});
		function groupCallback() {
		<xsl:apply-templates select="child::*[name() != 'ax:package']">
		  <xsl:with-param name='in-grp' select="1"/>
		</xsl:apply-templates>
		}
		</script>
	</xsl:template>

	<xsl:template match="ax:visualization">
	  <xsl:param name="in-grp"/>
	  <xsl:param name="in-layout"/>
		<xsl:choose>
			<xsl:when test="$in-grp = 1 or in-layout = 1">
			  <xsl:call-template name="visualization_constructor">
				<xsl:with-param name='in-grp' select="$in-grp"/>
			  </xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<script>
					<xsl:call-template name="visualization_constructor"/>
				</script>
			</xsl:otherwise>
			<!--xsl:otherwise>You need @package attribute in your visualization tag... &lt;ax:visualization id="<xsl:value-of select="@id"/>" package="<span style="font-weight:bold; font-style:italic">package_name</span>"... /&gt;</xsl:otherwise-->
		</xsl:choose>	
	</xsl:template>

	<xsl:template name="visualization_constructor">
	  	<xsl:param name="in-grp"/>
	/*
	 * Processing visualization <xsl:value-of select="@id"/>
	 */
		<xsl:if test="$in-grp != 1">
		google.load("visualization", "1", {<xsl:if test="@package">'packages':['<xsl:value-of select="@package"/>'], </xsl:if>'callback': <xsl:value-of select="@id"/>Callback});
		function <xsl:value-of select="@id"/>Callback() {
		</xsl:if>
		<xsl:value-of select="@id"/> = new <xsl:value-of select="@class"/>(<xsl:value-of select="@target"/>);
		<xsl:apply-templates select="ax:script"/>
		<xsl:variable name="src" select="@source"/>
		<xsl:variable name="vsrc" select="//ax:view[attribute::id=$src]/@source"/>
		<xsl:choose>
			<!--xsl:when test="@source and (//ax:query[attribute::id=$src] or (//ax:view[attribute::id=$src] and //ax:query[attribute::id=$vsrc]))">
		google.visualization.events.addListener(<xsl:value-of select="@source"/>, 'ready', function(arg) {
		  if(arg.DataTable) {
		    <xsl:value-of select="@id"/>DataCallback(arg.DataTable);
		  }
		});
			</xsl:when-->
			<xsl:when test="@source and (//ax:data[attribute::id=$src] or (//ax:view[attribute::id=$src] and //ax:data[attribute::id=$vsrc]))">
	    	<xsl:value-of select="@id"/>DataCallback(<xsl:value-of select="@source"/>);
			</xsl:when>
			<xsl:when test="ax:query | ax:data">
			  <xsl:apply-templates select="ax:query | ax:data">
			    <xsl:with-param name="id" select="@id"/>
			  </xsl:apply-templates>
			</xsl:when>
			<xsl:when test="ax:options or @options">
			  <xsl:value-of select="@id"/>.options = <xsl:choose><xsl:when test="@options">{<xsl:value-of select="@options"/>}</xsl:when><xsl:otherwise><xsl:apply-templates select="ax:options"/></xsl:otherwise></xsl:choose>
			</xsl:when>
		</xsl:choose>

		<xsl:if test="$in-grp != 1">
		}
		</xsl:if>
		function <xsl:value-of select="@id"/>DataCallback(data) {
			<xsl:value-of select="@id"/>.data = data;
			<xsl:value-of select="@id"/>.draw(data<xsl:if test="ax:options or @options">, <xsl:choose><xsl:when test="@options">{<xsl:value-of select="@options"/>}</xsl:when><xsl:otherwise><xsl:apply-templates select="ax:options"/></xsl:otherwise></xsl:choose></xsl:if>);
		}
	</xsl:template>

	<xsl:template match="ax:layout">
	  <xsl:param name="in-grp"/>
	  <xsl:param name="in-layout"/>
		<xsl:choose>
			<xsl:when test="$in-grp = 1 or in-layout = 1">
			  <xsl:call-template name="layout_constructor">
				<xsl:with-param name='in-grp' select="$in-grp"/>
			  </xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<script>
					<xsl:call-template name="layout_constructor"/>
				</script>
			</xsl:otherwise>
			<!--xsl:otherwise>You need @package attribute in your visualization tag... &lt;ax:visualization id="<xsl:value-of select="@id"/>" package="<span style="font-weight:bold; font-style:italic">package_name</span>"... /&gt;</xsl:otherwise-->
		</xsl:choose>	
	</xsl:template>

	<xsl:template name="layout_constructor">
	  <xsl:param name="in-grp"/>
	/*
	 * Processing layout <xsl:value-of select="@id"/>
	 */

		<xsl:value-of select="@id"/> = new <xsl:value-of select="@class"/>(<xsl:value-of select="@target"/>,<xsl:choose><xsl:when test="@callback"><xsl:value-of select="@callback"/></xsl:when><xsl:otherwise><xsl:value-of select="@id"/>Callback</xsl:otherwise></xsl:choose>);
		<xsl:apply-templates select="ax:script[not(attribute::place='callback')]"/>
		
		<xsl:value-of select="@id"/>.draw(<xsl:choose><xsl:when test="@options">{<xsl:value-of select="@options"/>}</xsl:when><xsl:otherwise><xsl:apply-templates select="ax:options"/></xsl:otherwise></xsl:choose>);
		function <xsl:value-of select="@id"/>Callback() {
			<xsl:apply-templates select="child::*[name() != 'ax:options' and name() != 'ax:script'] |  ax:script[attribute::place='callback']">
				<xsl:with-param name="in-grp" select="$in-grp"/>
				<xsl:with-param name="in-layout" select="1"/>
			</xsl:apply-templates>
		}

	</xsl:template>

	<xsl:template match="ax:gadget">
		<div class="gadget">
			<div class="gad_b4"/>
			<div class="gad_f gad_b3"/>
			<!--div class="gad_f gad_b2"/-->
			<div class="gad_f gad_b1"/>
			<div class="gadget_body">
				<div class="gadget_bar"><div style="margin-right: 3px;float:right;cursor: pointer" onclick="this.parentNode.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode.parentNode)">X</div></div>
				<div class="gadget_content">
					<script><xsl:attribute name="src"><xsl:value-of select="@source"/></xsl:attribute></script>
				</div>
			</div>
			<div class="gad_b1"/>
			<div class="gad_b3"/>
			<div class="gad_b4"/>
			<!--div class="gad_f gad_b2"/-->
		</div>
	</xsl:template>

	<!-- TEMPLATE FOR EVENT HANDLING -->
	<xsl:template match="ax:listener">
	  <xsl:param name="in-grp"/>
	  <xsl:param name="in-layout"/>
		<xsl:choose>
			<xsl:when test="$in-grp = 1 or in-layout = 1">
			  <xsl:call-template name="listener_constructor">
				<xsl:with-param name='in-grp' select="$in-grp"/>
			  </xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<script>
					<xsl:call-template name="listener_constructor"/>
				</script>
			</xsl:otherwise>
		</xsl:choose>	
	</xsl:template>

	<xsl:template name="listener_constructor">
	/*
	 * Processing Listener
	*/
		google.visualization.events.addListener(<xsl:value-of select="@id"/>, '<xsl:value-of select="@event"/>', <xsl:choose><xsl:when test="@function"><xsl:value-of select="@function"/></xsl:when><xsl:otherwise>function(event) {
		<xsl:value-of select="."/>
		}</xsl:otherwise></xsl:choose>);
	</xsl:template>


	<!-- TEMPLATES FOR DATA PROCESSING -->

	<xsl:template match="ax:data">
	  <xsl:param name="id"/>
	/*
	 * Processing data <xsl:value-of select="@id"/>
	*/
		<xsl:variable name="d"><xsl:choose><xsl:when test="@id"><xsl:value-of select="@id"/></xsl:when><xsl:otherwise>data</xsl:otherwise></xsl:choose></xsl:variable>
		<xsl:value-of select="$d"/> = new google.visualization.DataTable({
		'cols':[<xsl:for-each select="ax:col">{'id':'<xsl:value-of select="@id"/>', 'label':'<xsl:value-of select="@label"/>', 'type':'<xsl:value-of select="@type"/>'}<xsl:if test="position() != last()">, </xsl:if></xsl:for-each>],
		'rows':[<xsl:for-each select="ax:row">{'c':[<xsl:for-each select="ax:c"><xsl:variable name="pos" select="position()"/>{'v':<xsl:choose><xsl:when test="../../ax:col[position() = $pos]/@type = 'number'"><xsl:value-of select="@v"/></xsl:when><xsl:otherwise>'<xsl:value-of select="@v"/>'</xsl:otherwise></xsl:choose><xsl:if test="@f">, 'f':'<xsl:value-of select="@f"/>'</xsl:if>}<xsl:if test="position() != last()">, </xsl:if></xsl:for-each>]}<xsl:if test="position() != last()">, </xsl:if></xsl:for-each>]
		});
		<xsl:if test="$id"><xsl:value-of select="$id"/>DataCallback(<xsl:value-of select="$d"/>);</xsl:if>
	</xsl:template>

	<xsl:template match="ax:query">
	  <xsl:param name="id"/>
	/*
	 * Processing query <xsl:value-of select="@id"/>
	*/
		<xsl:variable name="q"><xsl:choose><xsl:when test="@id"><xsl:value-of select="@id"/></xsl:when><xsl:otherwise>query</xsl:otherwise></xsl:choose></xsl:variable>
		<xsl:value-of select="$q"/> = new google.visualization.Query('<xsl:value-of select="@source"/>'<xsl:if test="ax:options or @options">, <xsl:choose><xsl:when test="@options">{<xsl:value-of select="@options"/>}</xsl:when><xsl:otherwise><xsl:apply-templates select="ax:options"/></xsl:otherwise></xsl:choose></xsl:if>);
		<xsl:if test="@set"><xsl:value-of select="$q"/>.setQuery('<xsl:value-of select="@set"/>');</xsl:if>
		<xsl:choose><xsl:when test="$id"><xsl:value-of select="$q"/>.send(<xsl:value-of select="$id"/>DataCallback);</xsl:when><xsl:otherwise><xsl:value-of select="$q"/>.send(<xsl:value-of select="$q"/>QueryCallback);
		function <xsl:value-of select="$q"/>QueryCallback(response) {
			if(!response.isError()) {
			<xsl:for-each select="//ax:visualization[attribute::source=$q] | //ax:view[attribute::source=$q]">
			<xsl:value-of select="@id"/>DataCallback(response.getDataTable());
			</xsl:for-each>
			}
		}
		</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="ax:view">
	  <xsl:param name="id"/>
	/*
	 * Processing view <xsl:value-of select="@id"/>
	*/
		<xsl:variable name="v"><xsl:choose><xsl:when test="@id"><xsl:value-of select="@id"/></xsl:when><xsl:otherwise>view</xsl:otherwise></xsl:choose></xsl:variable>
		<xsl:variable name="src" select="@source"/>
		<xsl:if test="//ax:query[attribute::id=$src]">
		function <xsl:value-of select="@id"/>DataCallback(<xsl:value-of select="@source"/>) {
		</xsl:if>
		<xsl:value-of select="$v"/> = new google.visualization.DataView(<xsl:value-of select="@source"/>);
		<xsl:apply-templates select="ax:script"/>
		<xsl:choose><xsl:when test="$id"><xsl:value-of select="$id"/>DataCallback(<xsl:value-of select="$v"/>);</xsl:when>
		<xsl:when test="//ax:query[attribute::id=$src]"><xsl:for-each select="//ax:visualization[attribute::source=$v]">
		<xsl:value-of select="@id"/>DataCallback(data);
		</xsl:for-each></xsl:when></xsl:choose>
		<xsl:if test="//ax:query[attribute::id=$src]">
		}	
		</xsl:if>
	</xsl:template>

	<!-- COMMON AUXILIARY TEMPLATES -->

	<xsl:template match="ax:script">
		<xsl:value-of select="."/>
	</xsl:template>

	<xsl:template match="ax:options">{<xsl:for-each select="ax:o">'<xsl:value-of select="@n"/>':<xsl:choose><xsl:when test="not(@q) or @q='true'">'<xsl:value-of select="@v"/>'</xsl:when><xsl:otherwise><xsl:value-of select="@v"/></xsl:otherwise></xsl:choose><xsl:if test="position() != last()">, </xsl:if></xsl:for-each>}</xsl:template>

</xsl:stylesheet>
